package ar.edu.unsam.algo3.plantasvszombies.wicket;

import ar.edu.unsam.algo3.plantasvszombies.dominio.Batalla;
import ar.edu.unsam.algo3.plantasvszombies.dominio.Fila;
import ar.edu.unsam.algo3.plantasvszombies.dominio.Jardin;
import ar.edu.unsam.algo3.plantasvszombies.dominio.Zombie;
import ar.edu.unsam.algo3.plantasvszombies.ui.ApplicationModels.BatallaAppModel;
import java.util.ArrayList;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.Functions.Function0;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.uqbar.commons.model.UserException;
import org.uqbar.wicket.xtend.WicketExtensionFactoryMethods;
import org.uqbar.wicket.xtend.XAjaxLink;
import org.uqbar.wicket.xtend.XListView;

@SuppressWarnings("all")
public class BatallaPanel extends Panel {
  @Extension
  private WicketExtensionFactoryMethods _wicketExtensionFactoryMethods = new Function0<WicketExtensionFactoryMethods>() {
    public WicketExtensionFactoryMethods apply() {
      WicketExtensionFactoryMethods _wicketExtensionFactoryMethods = new WicketExtensionFactoryMethods();
      return _wicketExtensionFactoryMethods;
    }
  }.apply();
  
  private BatallaAppModel batallaApp;
  
  private FeedbackPanel feedbackPanel = new Function0<FeedbackPanel>() {
    public FeedbackPanel apply() {
      FeedbackPanel _feedbackPanel = new FeedbackPanel("feedbackPanel");
      return _feedbackPanel;
    }
  }.apply();
  
  public BatallaPanel(final String id, final Fila fila) {
    super(id);
    BatallaAppModel _batallaAppModel = new BatallaAppModel();
    this.batallaApp = _batallaAppModel;
    this.batallaApp.setFila(fila);
    Batalla _batalla = this.batallaApp.getBatalla();
    _batalla.setFila(fila);
    CompoundPropertyModel<BatallaAppModel> _compoundPropertyModel = new CompoundPropertyModel<BatallaAppModel>(this.batallaApp);
    Form<BatallaAppModel> _form = new Form<BatallaAppModel>("formBatalla", _compoundPropertyModel);
    final Form<BatallaAppModel> formBatalla = _form;
    formBatalla.setOutputMarkupId(true);
    this.agregarDatos(formBatalla);
    this.agregarTabla(formBatalla);
    this.agregarAcciones(formBatalla);
    this._wicketExtensionFactoryMethods.addChild(this, formBatalla);
    this.batallaApp.mostrar();
  }
  
  public MarkupContainer agregarDatos(final Form<BatallaAppModel> form) {
    MarkupContainer _xblockexpression = null;
    {
      Fila _fila = this.batallaApp.getFila();
      Jardin _jardin = _fila.getJardin();
      PropertyModel<Object> _propertyModel = new PropertyModel<Object>(_jardin, "recursos");
      Label _label = new Label("recursos", _propertyModel);
      this._wicketExtensionFactoryMethods.addChild(form, _label);
      this.feedbackPanel.setOutputMarkupId(true);
      MarkupContainer _addChild = this._wicketExtensionFactoryMethods.addChild(form, this.feedbackPanel);
      _xblockexpression = (_addChild);
    }
    return _xblockexpression;
  }
  
  public MarkupContainer agregarTabla(final Form<BatallaAppModel> form) {
    MarkupContainer _xblockexpression = null;
    {
      XListView<String> _xListView = new XListView<String>("resultado");
      final XListView<String> listView = _xListView;
      final Procedure1<ListItem<String>> _function = new Procedure1<ListItem<String>>() {
        public void apply(final ListItem<String> item) {
          String _modelObject = item.getModelObject();
          CompoundPropertyModel<String> _asCompoundModel = BatallaPanel.this._wicketExtensionFactoryMethods.<String>asCompoundModel(_modelObject);
          item.setModel(_asCompoundModel);
          String _modelObject_1 = item.getModelObject();
          Label _label = new Label("fila", _modelObject_1);
          BatallaPanel.this._wicketExtensionFactoryMethods.addChild(item, _label);
        }
      };
      listView.setPopulateItem(_function);
      MarkupContainer _addChild = this._wicketExtensionFactoryMethods.addChild(form, listView);
      _xblockexpression = (_addChild);
    }
    return _xblockexpression;
  }
  
  public MarkupContainer agregarAcciones(final Form<BatallaAppModel> form) {
    MarkupContainer _xblockexpression = null;
    {
      XListView<Zombie> _xListView = new XListView<Zombie>("result");
      final XListView<Zombie> listView = _xListView;
      final Procedure1<ListItem<Zombie>> _function = new Procedure1<ListItem<Zombie>>() {
        public void apply(final ListItem<Zombie> item) {
          Zombie _modelObject = item.getModelObject();
          CompoundPropertyModel<Zombie> _asCompoundModel = BatallaPanel.this._wicketExtensionFactoryMethods.<Zombie>asCompoundModel(_modelObject);
          item.setModel(_asCompoundModel);
          XAjaxLink<Object> _xAjaxLink = new XAjaxLink<Object>("seleccionar");
          final Procedure1<XAjaxLink<Object>> _function = new Procedure1<XAjaxLink<Object>>() {
            public void apply(final XAjaxLink<Object> it) {
              final Procedure1<AjaxRequestTarget> _function = new Procedure1<AjaxRequestTarget>() {
                public void apply(final AjaxRequestTarget target) {
                  try {
                    Batalla _batalla = BatallaPanel.this.batallaApp.getBatalla();
                    ArrayList<String> _arrayList = new ArrayList<String>();
                    _batalla.setResultado(_arrayList);
                    Zombie _modelObject = item.getModelObject();
                    BatallaPanel.this.batallaApp.setSelectedItem(_modelObject);
                    BatallaAppModel _modelObject_1 = form.getModelObject();
                    _modelObject_1.atacar();
                    target.add(form);
                  } catch (final Throwable _t) {
                    if (_t instanceof UserException) {
                      final UserException e = (UserException)_t;
                      String _message = e.getMessage();
                      it.error(_message);
                      target.add(BatallaPanel.this.feedbackPanel);
                    } else {
                      throw Exceptions.sneakyThrow(_t);
                    }
                  }
                }
              };
              it.setOnClick(_function);
            }
          };
          final XAjaxLink<Object> miBoton = ObjectExtensions.<XAjaxLink<Object>>operator_doubleArrow(_xAjaxLink, _function);
          Label _label = new Label("nombre");
          BatallaPanel.this._wicketExtensionFactoryMethods.addChild(miBoton, _label);
          Label _label_1 = new Label("defensa");
          BatallaPanel.this._wicketExtensionFactoryMethods.addChild(miBoton, _label_1);
          Label _label_2 = new Label("ataque");
          BatallaPanel.this._wicketExtensionFactoryMethods.addChild(miBoton, _label_2);
          BatallaPanel.this._wicketExtensionFactoryMethods.addChild(item, miBoton);
        }
      };
      listView.setPopulateItem(_function);
      XAjaxLink<Object> _xAjaxLink = new XAjaxLink<Object>("salir");
      final Procedure1<XAjaxLink<Object>> _function_1 = new Procedure1<XAjaxLink<Object>>() {
        public void apply(final XAjaxLink<Object> it) {
          final Procedure1<AjaxRequestTarget> _function = new Procedure1<AjaxRequestTarget>() {
            public void apply(final AjaxRequestTarget target) {
              MarkupContainer _parent = BatallaPanel.this.getParent();
              target.add(_parent);
            }
          };
          it.setOnClick(_function);
        }
      };
      XAjaxLink<Object> _doubleArrow = ObjectExtensions.<XAjaxLink<Object>>operator_doubleArrow(_xAjaxLink, _function_1);
      this._wicketExtensionFactoryMethods.addChild(form, _doubleArrow);
      MarkupContainer _addChild = this._wicketExtensionFactoryMethods.addChild(form, listView);
      _xblockexpression = (_addChild);
    }
    return _xblockexpression;
  }
}
